package com.bdcom.nms.NmsCollect.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;

import com.bdcom.nms.NmsCollect.App;
import com.bdcom.nms.NmsCollect.dao.CollectResultPersistDao;
import com.bdcom.nms.NmsCollect.pojo.IfEntry;
/**
 * 此类作为采集结果的生产消费类
 * 
 * @author Administrator
 *
 */
public class BdcomSwitchPersistThread extends Thread {

	private BlockingQueue<Object> queue;
	
	private CollectResultPersistDao collectresultpersistdao;
	
	static  Logger _log=Logger.getLogger(BdcomSwitchPersistThread.class);

	public BdcomSwitchPersistThread() {
		super();
		// TODO Auto-generated constructor stub
		
		queue = new LinkedBlockingQueue<Object>();
		
		this.start();
	}

	/**
	 * 消费阻塞队列里的对象
	 */
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
	
		Object o = null;
		
		while (true) {
			try {
				o = queue.take();
				
				if(collectresultpersistdao==null){
					
						collectresultpersistdao=(CollectResultPersistDao) App.app_context.getBean("collectresultpersistdao");
						
				}
				
				if(o instanceof IfEntry){
					
					((IfEntry) o).setIfPhysAddress();
					
				}
				
				collectresultpersistdao.PersistObject(o);
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				_log.error("take Object Exception!:"+e.getLocalizedMessage());
				
			}
		}
	}

	/**
	 * 将采集结果放入阻塞队列
	 * 
	 * @param o
	 */
	public synchronized void execute(Object o) {
		try {
			
			queue.put(o);
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			_log.error("put Object Exception!:"+e.getLocalizedMessage());
		}
	}

}
